package com.ionehe.group.common.uaa.ex.application.rest;

import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.application.converter.FunctionVOConvert;
import com.ionehe.group.common.uaa.ex.application.converter.SetMealConverter;
import com.ionehe.group.common.uaa.ex.application.model.FunctionVO;
import com.ionehe.group.common.uaa.ex.application.model.SetMealDeleteVO;
import com.ionehe.group.common.uaa.ex.application.model.SetMealVO;
import com.ionehe.group.common.uaa.ex.domain.service.FunctionReadService;
import com.ionehe.group.common.uaa.ex.domain.service.SetMealReadService;
import com.ionehe.group.common.uaa.ex.domain.service.SetMealWriteService;
import com.ionehe.group.common.uaa.ex.dto.FunctionDTO;
import com.ionehe.group.common.uaa.ex.dto.Paging;
import com.ionehe.group.common.uaa.ex.dto.SetMealDTO;
import com.ionehe.group.common.uaa.ex.dto.SetMealListNotPageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/**
 * @USER: ls
 * @DATE: 2020/11/24
 * @PROJECT_NAME: framework
 * @Description 套餐管理
 **/

@RestController
@RequestMapping("/api/uaa/setMeal")
@Api(hidden = true, value = "uaa-套餐管理相关服务",tags = "uaa-套餐管理相关服务")
@ApiIgnore
public class UaaSetMealController {
    @Autowired
    SetMealConverter setMealConverter;
    @Autowired
    SetMealWriteService setMealWriteService;
    @Autowired
    SetMealReadService setMealReadService;
    @Autowired
    private FunctionReadService functionReadService;

    @ApiOperation("修改或添加套餐")
    @PostMapping("/addAndUpdate")
    public Response setMealAddAndUpdate(@RequestBody @ApiParam("套餐修改添加类") SetMealVO setMealVO) {
        return setMealWriteService.setMealAddAndUpdate(setMealConverter.vo2dto(setMealVO));
    }

    @ApiOperation("套餐列表")
    @GetMapping("/list")
    public Response<Paging<SetMealVO>> setMealList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) {
        Response<Paging<SetMealDTO>> res = setMealReadService.setMealList(pageNum, pageSize);
        if (!res.success()) {
            return Response.no(res.getMessage());
        }
        Paging<SetMealDTO> data = res.getData();
        if (data.isEmpty()) {
            return Response.yes(Paging.empty());
        }

        Paging<SetMealVO> paging = new Paging<>();
        paging.setData(setMealConverter.dto2vo(data.getData()));
        paging.setTotal(paging.getTotal());
        return Response.yes(paging);
    }

    /**
     * 功能列表
     *
     * @return 添加套餐功能列表
     */
    @GetMapping("/addSetMealFunctionList")
    @ApiOperation("添加套餐功能列表")
    public Response<List<FunctionVO>> functionDetailInfo() {
        Response<List<FunctionDTO>> res = functionReadService.addSetMealFunctionList(null);
        if (!res.success()) {
            return Response.no(res.getMessage());
        }
        Response<List<FunctionVO>> yes = Response.yes(FunctionVOConvert.INSTANCE.dto2vo(res.getData()));
        yes.setMessage(null);
        return yes;
    }


    @ApiOperation("不分页套餐列表")
    @GetMapping("/listNotPage")
    public Response<List<SetMealListNotPageDTO>> setMealListNotPage(
    ) {
        return setMealReadService.setMealListNotPage();
    }


    @ApiOperation("套餐删除")
    @PostMapping("/delete")
    public Response setMealDelete(@RequestBody @ApiParam("套餐删除类") SetMealDeleteVO setMealDeleteVO) {
        return  setMealWriteService.setMealDelete(setMealDeleteVO.getSetMealId());
    }


    @ApiOperation("套餐删除判断")
    @GetMapping("/deleteJudge")
    public Response deleteJudge(Long setMealId) {
        return  setMealWriteService.deleteJudge(setMealId);
    }


}
